package com.iotproject.educenter.controller;


import com.iotproject.commonutils.JwtUtils;
import com.iotproject.commonutils.R;
import com.iotproject.commonutils.forumvo.UcenterMemberForum;
import com.iotproject.commonutils.ordervo.UcenterMemberOrder;
import com.iotproject.educenter.entity.Member;
import com.iotproject.educenter.entity.vo.RegisterVo;
import com.iotproject.educenter.entity.vo.UpdateAvatarVo;
import com.iotproject.educenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-11-12
 */
@RestController
@RequestMapping("/educenter/ucenter")
//d @CrossOrigin
public class UcenterMemberController {

    @Autowired
    UcenterMemberService ucenterMemberService;

    //登录功能
    @PostMapping("login")
    public R login(@RequestBody Member member) {
        String token = ucenterMemberService.login(member);
        return R.ok().data("token", token);
    }

    //注册功能
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo) {
        ucenterMemberService.register(registerVo);
        return R.ok();
    }

    //根据token(位于请求头)获取用户信息
    @GetMapping("getMemberInfo")
    public R getMemberInfo(HttpServletRequest httpServletRequest) {
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(httpServletRequest);
        System.out.println("id: " + memberIdByJwtToken);
        Member member = ucenterMemberService.getById(memberIdByJwtToken);
        return R.ok().data("userInfo", member);
    }

    //根据用户id获取用户信息(订单使用)
    @PostMapping("getUserInfoOrder/{id}")
    public UcenterMemberOrder getUserInfoOrder(@PathVariable String id) {
        Member member = ucenterMemberService.getById(id);
        //把member对象里面值复制给UcenterMemberOrder对象
        UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
        BeanUtils.copyProperties(member,ucenterMemberOrder);
        return ucenterMemberOrder;
    }

    //根据用户id获取用户信息(论坛使用)
    @PostMapping("getUserInfoForum/{id}")
    public UcenterMemberForum getUserInfoForum(@PathVariable String id) {
        Member member = ucenterMemberService.getById(id);
        //把member对象里面值复制给UcenterMemberOrder对象
        UcenterMemberForum ucenterMemberForum = new UcenterMemberForum();
        BeanUtils.copyProperties(member, ucenterMemberForum);
        return ucenterMemberForum;
    }

    //查询某一天注册人数（实际上就是某一天里ucenter_memeber表的人数）
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day) {
        Integer count = ucenterMemberService.countRegisterDay(day);
        return R.ok().data("countRegister",count);
    }

    //修改用户昵称
    @PostMapping("updateNickname/{userId}/{newName}")
    public R updateNickname(@PathVariable("userId") String userId, @PathVariable("newName") String newName) {
        ucenterMemberService.updateNickname(userId, newName);
        return R.ok();
    }

    //修改用户头像
    @PostMapping("updateAvatar")
    public R updateAvatar(@RequestBody UpdateAvatarVo updateAvatarVo) {
        ucenterMemberService.updateAvatar(updateAvatarVo.getId(), updateAvatarVo.getAvatar());
        return R.ok();
    }

    //修改用户密码
    @PostMapping("updatePassword/{userId}/{newPassword}")
    public R updatePassword(@PathVariable("userId") String userId, @PathVariable("newPassword") String newPassword) {
        ucenterMemberService.updatePassword(userId, newPassword);
        return R.ok();
    }

    //修改手机号码
    @PostMapping("updateMobile/{userId}/{newMobile}")
    public R updateMobile(@PathVariable("userId") String userId, @PathVariable("newMobile") String newMobile) {
        ucenterMemberService.updateMobile(userId, newMobile);
        return R.ok();
    }

    //查询某一天登陆人数（实际上就是某一天里ucenter_memeber表的人数）
    @GetMapping("countLogin/{day}")
    public R countLogin(@PathVariable String day) {
        Integer count = ucenterMemberService.countLoginDay(day);
        return R.ok().data("countLogin",count);
    }

}

